﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services
{
    public class DepartMentService: IDepartMentService
    {


        private readonly IDepartMentRepository departRe;
        private readonly ILogger<DepartMentService> logger;
        private readonly IMapper imapper;

        public DepartMentService(IDepartMentRepository departRe, ILogger<DepartMentService> logger, IMapper imapper)
        {
            this.departRe = departRe;
            this.logger = logger;
            this.imapper = imapper;
        }


        /// <summary>
        /// 部门列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageOutput<DepartMentOutPut>> DepartMnetList(DepartMentInPut input)
        {
            try
            {   logger.LogInformation("分页查询部门列表");
                var list = await departRe.DepartMnet();
                if (!string.IsNullOrEmpty(input.DepartName))
                {
                    list = list.Where(c => c.DepartName.Contains(input.DepartName));
                }
                if (input.DepartTypeId > 0)
                {
                    list = list.Where(c => c.DepartTypeId == input.DepartTypeId);

                }
                if (input.DepartState > 0)
                {
                    list = list.Where(c => c.DepartState == input.DepartState);
                }

                var result = new PageOutput<DepartMentOutPut>
                {
                    TotalCount = list.Count(),
                    PageSize = input.PageSize,
                };

                list = list.OrderBy(c => c.DepartId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
                result.Data = imapper.Map<List<DepartMentOutPut>>(list);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }


           

        }


        /// <summary>
        /// 部门添加
        /// </summary>
        /// <param name="depart"></param>
        /// <returns></returns>
        public async Task<int> Add(Department depart)
        {
            try
            {
                logger.LogInformation("添加部门");
                //bool isUnique = await IsDepartmentNameUnique(depart.DepartName);
                //if (!isUnique)
                //{
                //    throw new ArgumentException("部门名称已经存在，请使用其他名称。");
      
                //}
                depart.CreateTime = DateTime.Now;
                depart.UpdateTime = DateTime.Now;
                var entity = imapper.Map<Department>(depart);

                return await departRe.Add(entity);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
      
        }

  
        /// <summary>
        /// 树形
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public async Task<List<MenuList>> MenuLIst(int pid)
        {

            try
            {    logger.LogInformation("查询菜单列表");
                var entity =  await departRe.MenuLIst(pid);
                return entity;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
           
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="departId"></param>
        /// <returns></returns>
        public async Task<int> Delete(int departId)
        {
            return await departRe.Delete(departId);
        }





    }




}
